|
1
|
|
|
/** |
|
2
|
|
|
* bit-parser: Functions to read and write bytes. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
* Float32 based on int-bits: https://github.com/Jam3/int-bits |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
const helpers = require("../src/helpers.js"); |
|
9
|
|
|
const floats = require("../src/floats.js"); |
|
10
|
|
|
let i8 = new Int8Array(4); |
|
11
|
|
|
let i32 = new Int32Array(i8.buffer, 0, 1); |
|
12
|
|
|
let f32 = new Float32Array(i8.buffer, 0, 1); |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Read a group of bytes by turning it to bits. |
|
16
|
|
|
* Useful for 40 & 48-bit, but underperform. |
|
17
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
18
|
|
|
* @param {number} i The index to read. |
|
19
|
|
|
* @param {number} numBytes The number of bytes |
|
20
|
|
|
* (1 for 8-bit, 2 for 16-bit, etc). |
|
21
|
|
|
* @return {number} |
|
22
|
|
|
*/ |
|
23
|
|
|
function readBytesAsBits(bytes, i, numBytes) { |
|
24
|
|
|
let j = numBytes-1; |
|
25
|
|
|
let bits = ""; |
|
26
|
|
|
while (j >= 0) { |
|
27
|
|
|
bits += helpers.bytePadding(bytes[j + i].toString(2), 2); |
|
28
|
|
|
j--; |
|
29
|
|
|
} |
|
30
|
|
|
return parseInt(bits, 2); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
let BitReader = { |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Read 1 8-bit int from from bytes. |
|
37
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
38
|
|
|
* @param {number} i The index to read. |
|
39
|
|
|
* @return {number} |
|
40
|
|
|
*/ |
|
41
|
|
|
"read8Bit": function (bytes, i) { |
|
42
|
|
|
return bytes[i]; |
|
43
|
|
|
}, |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Read 1 16-bit int from from bytes. |
|
47
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
48
|
|
|
* @param {number} i The index to read. |
|
49
|
|
|
* @return {number} |
|
50
|
|
|
*/ |
|
51
|
|
|
"read16Bit": function (bytes, i) { |
|
52
|
|
|
return bytes[1 + i] << 8 | bytes[i]; |
|
53
|
|
|
}, |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Read 1 16-bit float from from bytes. |
|
57
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
58
|
|
|
* @param {number} i The index to read. |
|
59
|
|
|
* @return {number} |
|
60
|
|
|
*/ |
|
61
|
|
|
"read16BitFloat": function (bytes, i) { |
|
62
|
|
|
return floats.decodeFloat16(bytes.slice(i,i+2)); |
|
63
|
|
|
}, |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Read 1 24-bit int from from bytes. |
|
67
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
68
|
|
|
* @param {number} i The index to read. |
|
69
|
|
|
* @return {number} |
|
70
|
|
|
*/ |
|
71
|
|
|
"read24Bit": function (bytes, i) { |
|
72
|
|
|
return bytes[2 + i] << 16 | BitReader["read16Bit"](bytes, i); |
|
73
|
|
|
}, |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Read 1 32-bit int from from bytes. |
|
77
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
78
|
|
|
* @param {number} i The index to read. |
|
79
|
|
|
* @return {number} |
|
80
|
|
|
*/ |
|
81
|
|
|
"read32Bit": function (bytes, i) { |
|
82
|
|
|
return (bytes[3 + i] << 24 | |
|
83
|
|
|
BitReader["read24Bit"](bytes, i)) >>> 0; |
|
84
|
|
|
}, |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Read 1 32-bit float from from bytes. |
|
88
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
89
|
|
|
* @param {number} i The index to read. |
|
90
|
|
|
* @return {number} |
|
91
|
|
|
*/ |
|
92
|
|
|
"read32BitFloat": function (bytes, i) { |
|
93
|
|
|
i32[0] = BitReader["read32Bit"](bytes, i); |
|
94
|
|
|
return f32[0]; |
|
95
|
|
|
}, |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Read 1 40-bit int from from bytes. |
|
99
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
100
|
|
|
* @param {number} i The index to read. |
|
101
|
|
|
* @return {number} |
|
102
|
|
|
*/ |
|
103
|
|
|
"read40Bit": function (bytes, i) { |
|
104
|
|
|
return readBytesAsBits(bytes, i, 5); |
|
105
|
|
|
}, |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Read 1 48-bit int from bytes. |
|
109
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
110
|
|
|
* @param {number} i The index to read. |
|
111
|
|
|
* @return {number} |
|
112
|
|
|
*/ |
|
113
|
|
|
"read48Bit": function (bytes, i) { |
|
114
|
|
|
return readBytesAsBits(bytes, i, 6); |
|
115
|
|
|
}, |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Read 1 64-bit double from bytes. |
|
119
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
120
|
|
|
* @param {number} i The index to read. |
|
121
|
|
|
* @return {number} |
|
122
|
|
|
*/ |
|
123
|
|
|
"read64BitFloat": function (bytes, i) { |
|
124
|
|
|
return floats.decodeFloat64(bytes.slice(i,i+8)); |
|
125
|
|
|
}, |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Read 1 char from bytes. |
|
129
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
130
|
|
|
* @param {number} i The index to read. |
|
131
|
|
|
* @return {string} |
|
132
|
|
|
*/ |
|
133
|
|
|
"readChar": function (bytes, i, type) { |
|
134
|
|
|
let chrs = ""; |
|
135
|
|
|
let j = 0; |
|
136
|
|
|
let len = type.bits / 8; |
|
137
|
|
|
while(j < len) { |
|
138
|
|
|
chrs += String.fromCharCode(bytes[i+j]); |
|
139
|
|
|
j++; |
|
140
|
|
|
} |
|
141
|
|
|
return chrs; |
|
142
|
|
|
} |
|
143
|
|
|
}; |
|
144
|
|
|
|
|
145
|
|
|
let BitWriter = { |
|
146
|
|
|
|
|
147
|
|
|
"write64BitFloat": function(bytes, number, j) { |
|
148
|
|
|
let bits = floats.toFloat64(number); |
|
149
|
|
|
j = BitWriter["write32Bit"](bytes, bits[1], j); |
|
150
|
|
|
return BitWriter["write32Bit"](bytes, bits[0], j); |
|
151
|
|
|
}, |
|
152
|
|
|
|
|
153
|
|
|
// Thanks https://github.com/majimboo/c-struct |
|
154
|
|
|
"write48Bit": function (bytes, number, j) { |
|
155
|
|
|
j = BitWriter["write40Bit"](bytes, number, j); |
|
156
|
|
|
bytes[j++] = number / 0x10000000000 & 0xFF; |
|
157
|
|
|
return j; |
|
158
|
|
|
}, |
|
159
|
|
|
|
|
160
|
|
|
// Thanks https://github.com/majimboo/c-struct |
|
161
|
|
|
"write40Bit": function (bytes, number, j) { |
|
162
|
|
|
j = BitWriter["write32Bit"](bytes, number, j); |
|
163
|
|
|
bytes[j++] = number / 0x100000000 & 0xFF; |
|
164
|
|
|
return j; |
|
165
|
|
|
}, |
|
166
|
|
|
|
|
167
|
|
|
"write32BitFloat": function (bytes, number, j) { |
|
168
|
|
|
f32[0] = number; |
|
169
|
|
|
j = BitWriter["write32Bit"](bytes, i32[0], j); |
|
170
|
|
|
return j; |
|
171
|
|
|
}, |
|
172
|
|
|
|
|
173
|
|
|
"write32Bit": function (bytes, number, j) { |
|
174
|
|
|
j = BitWriter["write24Bit"](bytes, number, j); |
|
175
|
|
|
bytes[j++] = number >>> 24 & 0xFF; |
|
176
|
|
|
return j; |
|
177
|
|
|
}, |
|
178
|
|
|
|
|
179
|
|
|
"write24Bit": function (bytes, number, j) { |
|
180
|
|
|
j = BitWriter["write16Bit"](bytes, number, j); |
|
181
|
|
|
bytes[j++] = number >>> 16 & 0xFF; |
|
182
|
|
|
return j; |
|
183
|
|
|
}, |
|
184
|
|
|
|
|
185
|
|
|
"write16Bit": function (bytes, number, j) { |
|
186
|
|
|
bytes[j++] = number & 0xFF; |
|
187
|
|
|
bytes[j++] = number >>> 8 & 0xFF; |
|
188
|
|
|
return j; |
|
189
|
|
|
}, |
|
190
|
|
|
|
|
191
|
|
|
"write16BitFloat": function (bytes, number, j) { |
|
192
|
|
|
let bits = floats.toHalf(number); |
|
193
|
|
|
bytes[j++] = bits >>> 8 & 0xFF; |
|
194
|
|
|
bytes[j++] = bits & 0xFF; |
|
195
|
|
|
return j; |
|
196
|
|
|
}, |
|
197
|
|
|
|
|
198
|
|
|
"write8Bit": function (bytes, number, j) { |
|
199
|
|
|
bytes[j++] = number & 0xFF; |
|
200
|
|
|
return j; |
|
201
|
|
|
}, |
|
202
|
|
|
|
|
203
|
|
|
"write4Bit": function (bytes, number, j) { |
|
204
|
|
|
bytes[j++] = number & 0xF; |
|
205
|
|
|
return j; |
|
206
|
|
|
}, |
|
207
|
|
|
|
|
208
|
|
|
"write2Bit": function (bytes, number, j) { |
|
209
|
|
|
bytes[j++] = number < 0 ? number + 4 : number; |
|
210
|
|
|
return j; |
|
211
|
|
|
}, |
|
212
|
|
|
|
|
213
|
|
|
"write1Bit": function (bytes, number, j) { |
|
214
|
|
|
bytes[j++] = number ? 1 : 0; |
|
215
|
|
|
return j; |
|
216
|
|
|
}, |
|
217
|
|
|
|
|
218
|
|
|
"writeString": function (bytes, string, j) { |
|
219
|
|
|
bytes[j++] = string.charCodeAt(0); |
|
220
|
|
|
return j; |
|
221
|
|
|
} |
|
222
|
|
|
}; |
|
223
|
|
|
|
|
224
|
|
|
module.exports.BitWriter = BitWriter; |
|
225
|
|
|
module.exports.BitReader = BitReader; |
|
226
|
|
|
|